package com.android.wnba.util;

import com.android.wnba.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DialogUtil {

	public static interface OnProgressDialogListener {
		public void onProgressDialogCancel(int id);
	}

	public static interface WarningDialogListener {
		public void onWarningDialogCancel(int id);

		public void onWarningDialogOK(int id);
	}

	public static interface InputDialogListener {
		public void onInputDialogCancel(int id);

		public void onInputDialogOK(int id, String value);
	}

	/**
	 * <p>
	 * Create a warning dialog which only has one OK button
	 * </p>
	 * 
	 * @param context
	 *            {@link Context} object
	 * @param id
	 *            dialog id
	 * @param warning
	 *            warning message
	 * @return a warning dialog instance
	 */
	public static Dialog createOKWarningDialog(final Context context,
			final int id, final String title, String warning,
			final WarningDialogListener listener) {
		/*
		 * Common warning dialog Only one OK buttons, one message, an icon
		 */
		AlertDialog.Builder builder = new AlertDialog.Builder(context)
				.setCancelable(false);
		if ((null != title) && (!"".equals(title))) {
			builder.setTitle(title);
		} else {
			builder.setTitle(R.string.pub_attention);
		}
		builder.setMessage(warning)
				.setPositiveButton(R.string.pub_confirm,
						new DialogInterface.OnClickListener() {

							// //@Override
							public void onClick(DialogInterface dialog,
									int whichButton) {
								if (context instanceof Activity)
									((Activity) context).removeDialog(id);

								if (listener != null)
									listener.onWarningDialogOK(id);
							}
						})
				.setOnCancelListener(new DialogInterface.OnCancelListener() {

					// //@Override
					public void onCancel(DialogInterface arg0) {
						if (context instanceof Activity)
							((Activity) context).removeDialog(id);

						if (listener != null)
							listener.onWarningDialogOK(id);
					}
				}).setOnKeyListener(new OnKeyListener() {
					public boolean onKey(DialogInterface arg0, int arg1,
							KeyEvent arg2) {
						switch (arg1) {
						case KeyEvent.KEYCODE_SEARCH:
							return true;
						default:
							break;
						}
						return false;
					}
				});
		return builder.create();
	}

	public static Dialog createIndeterminateProgressDialog(
			final Context context, final int id, String hint,
			boolean cancelable, final OnProgressDialogListener listener) {
		LayoutInflater factory = LayoutInflater.from(context);
		View view = factory.inflate(
				R.layout.alert_dialog_indeterminate_progress, null);

		// set hint
		if (hint == null) {
			throw new RuntimeException(
					"Must provide a hint string for input dialog");
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(context)
				.setTitle(hint).setCancelable(true).setView(view);

		builder.setCancelable(cancelable);

		if (cancelable) {
			builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
				public void onCancel(DialogInterface arg0) {
					if (context instanceof Activity)
						((Activity) context).removeDialog(id);

					if (listener != null)
						listener.onProgressDialogCancel(id);
				}
			});
		}

		Dialog dialog = builder.create();

		if (!cancelable) {
			dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
				public void onDismiss(DialogInterface arg0) {
					if (context instanceof Activity)
						((Activity) context).removeDialog(id);
				}
			});
		}

		dialog.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
				switch (arg1) {
				case KeyEvent.KEYCODE_SEARCH:
					return true;
				default:
					break;
				}
				return false;
			}
		});
		return dialog;
	}

	/**
	 * <p>
	 * Create a warning dialog
	 * </p>
	 * 
	 * 
	 * @param context
	 *            {@link Context} object
	 * @param id
	 *            dialog id
	 * @param title
	 *            warnig titile
	 * @param message
	 *            warning message
	 * @param listener
	 *            {@link WarningDialogListener} instance, should not be null
	 * @return a warning dialog instance
	 * 
	 */
	public static Dialog createRetryWarningDialog(final Context context,
			final int id, final String title, final int message,
			final WarningDialogListener listener) {
		/*
		 * Common warning dialog Two buttons, one message, an icon
		 */
		Builder builder = new AlertDialog.Builder(context);
		if ((null != title) && (!"".equals(title))) {
			builder.setTitle(title);
		} else {
			builder.setTitle(R.string.pub_attention);
		}
		if (message != -1) {
			builder.setMessage(message);
		}
		builder.setPositiveButton(R.string.pub_retry,
				new DialogInterface.OnClickListener() {

					// //@Override
					public void onClick(DialogInterface dialog, int whichButton) {
						if (context instanceof Activity) {
							((Activity) context).removeDialog(id);
						}

						if (listener != null)
							listener.onWarningDialogOK(id);
					}
				})
				.setNegativeButton(R.string.pub_cancel,
						new DialogInterface.OnClickListener() {

							// //@Override
							public void onClick(DialogInterface dialog,
									int whichButton) {
								if (context instanceof Activity) {
									((Activity) context).removeDialog(id);

									if (listener != null)
										listener.onWarningDialogCancel(id);
								}
							}
						}).setCancelable(false)
				.setOnKeyListener(new OnKeyListener() {
					public boolean onKey(DialogInterface arg0, int arg1,
							KeyEvent arg2) {
						switch (arg1) {
						case KeyEvent.KEYCODE_SEARCH:
							return true;
						default:
							break;
						}
						return false;
					}
				});
		return builder.create();
	}

	/**
	 * <p>
	 * Create a warning dialog
	 * </p>
	 * 
	 * 
	 * @param context
	 *            {@link Context} object
	 * @param id
	 *            dialog id
	 * @param title
	 *            warnig titile
	 * @param message
	 *            warning message
	 * @param listener
	 *            {@link WarningDialogListener} instance, should not be null
	 * @return a warning dialog instance
	 * 
	 */
	public static Dialog createYesNoWarningDialog(final Context context,
			final int id, final String title, final int message,
			final WarningDialogListener listener, final View view) {
		/*
		 * Common warning dialog Two buttons, one message, an icon
		 */
		Builder builder = new AlertDialog.Builder(context);
		if ((null != title) && (!"".equals(title))) {
			builder.setTitle(title);
		} else {
			builder.setTitle(R.string.pub_attention);
		}
		if (null != view) {
			builder.setView(view);
		}
		if (message != -1) {
			builder.setMessage(message);
		}
		builder.setPositiveButton(R.string.pub_confirm,
				new DialogInterface.OnClickListener() {

					// //@Override
					public void onClick(DialogInterface dialog, int whichButton) {
						if (context instanceof Activity) {
							((Activity) context).removeDialog(id);
						}

						if (listener != null)
							listener.onWarningDialogOK(id);
					}
				})
				.setNegativeButton(R.string.pub_cancel,
						new DialogInterface.OnClickListener() {

							// //@Override
							public void onClick(DialogInterface dialog,
									int whichButton) {
								if (context instanceof Activity) {
									((Activity) context).removeDialog(id);

									if (listener != null)
										listener.onWarningDialogCancel(id);
								}
							}
						})
				.setOnCancelListener(new DialogInterface.OnCancelListener() {

					// //@Override
					public void onCancel(DialogInterface arg) {
						if (context instanceof Activity) {
							((Activity) context).removeDialog(id);
						}
					}
				}).setOnKeyListener(new OnKeyListener() {
					public boolean onKey(DialogInterface arg0, int arg1,
							KeyEvent arg2) {
						switch (arg1) {
						case KeyEvent.KEYCODE_SEARCH:
							return true;
						default:
							break;
						}
						return false;
					}
				});
		return builder.create();
	}

	/**
	 * <p>
	 * Create an input dialog
	 * </p>
	 * 
	 * @param context
	 *            {@link Context} object
	 * @param id
	 *            dialog id
	 * @param hint
	 *            hint message
	 * @param initValue
	 *            initial string value in text box, can be null if no initial
	 *            value
	 * @param secure
	 *            true if you want to input password
	 * @param listener
	 *            {@link InputDialogListener} instance, should not be null
	 * @return an input dialog instance
	 */
	public static Dialog createInputDialog(final Context context, final int id,
			String hint, String initValue, boolean secure,
			final InputDialogListener listener) {
		return createInputDialog(context, id,
				context.getString(R.string.pub_dialog_input_title), hint,
				initValue, secure, listener);
	}

	/**
	 * <p>
	 * Create an input dialog
	 * </p>
	 * 
	 * @param context
	 *            {@link Context} object
	 * @param id
	 *            dialog id
	 * @param hint
	 *            hint message
	 * @param initValue
	 *            initial string value in text box, can be null if no initial
	 *            value
	 * @param secure
	 *            true if you want to input password
	 * @param listener
	 *            {@link InputDialogListener} instance, should not be null
	 * @return an input dialog instance
	 */
	public static Dialog createInputDialog(final Context context, final int id,
			String title, String hint, String initValue, boolean secure,
			final InputDialogListener listener) {
		/*
		 * Common input dialog Two buttons, one edittext, one hint, and title
		 */

		LayoutInflater factory = LayoutInflater.from(context);
		View view = factory.inflate(R.layout.alert_dialog_text_entry, null);

		// set hint
		if (hint == null) {
			throw new RuntimeException(
					"Must provide a hint string for input dialog");
		}
		TextView tvHint = (TextView) view.findViewById(R.id.tv_hint);
		tvHint.setText(hint);

		// set init value
		final EditText etInput = (EditText) view.findViewById(R.id.et_input);
		if (initValue != null) {
			etInput.setText(initValue);
		}
		if (secure)
			etInput.setTransformationMethod(PasswordTransformationMethod
					.getInstance());

		final AlertDialog dialog = new AlertDialog.Builder(context)
				.setIcon(android.R.drawable.ic_dialog_info)
				.setTitle(title)
				.setView(view)
				.setPositiveButton(R.string.pub_confirm,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								String value = etInput.getText().toString();
								if (context instanceof Activity) {
									((Activity) context).removeDialog(id);
								}

								if (listener != null)
									listener.onInputDialogOK(id, value);
							}
						})
				.setNegativeButton(R.string.pub_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								if (context instanceof Activity) {
									((Activity) context).removeDialog(id);
								}

								if (listener != null)
									listener.onInputDialogCancel(id);
							}
						})
				.setOnCancelListener(new DialogInterface.OnCancelListener() {
					public void onCancel(DialogInterface arg0) {
						if (context instanceof Activity) {
							((Activity) context).removeDialog(id);
						}

						if (listener != null)
							listener.onInputDialogCancel(id);
					}
				}).setOnKeyListener(new OnKeyListener() {
					public boolean onKey(DialogInterface arg0, int arg1,
							KeyEvent arg2) {
						switch (arg1) {
						case KeyEvent.KEYCODE_SEARCH:
							return true;
						default:
							break;
						}
						return false;
					}
				}).create();

		etInput.addTextChangedListener(new TextWatcher() {
			// @Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(
						(null != s) && !"".equals(s.toString().trim()));
			}

			// @Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			// @Override
			public void afterTextChanged(Editable s) {
			}
		});

		return dialog;
	}
}
